package com.teemor.blog.controller;


import com.alibaba.fastjson.JSON;
import com.teemor.blog.api.IArticleApiService;
import com.teemor.blog.bean.condition.TbArticleCondition;
import com.teemor.blog.bean.dto.output.article.QryArchivesODTo;
import com.teemor.blog.bean.entity.TbArticle;
import com.teemor.blog.bean.dto.input.article.AddArticleIDTo;
import com.teemor.blog.bean.dto.input.article.QryArticlesIDTo;
import com.teemor.blog.bean.dto.input.article.UpdArticleIDTo;
import com.teemor.blog.service.IArticleExtendService;
import com.teemor.blog.service.IArticleService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teemor.common.bean.utils.BeanConvertor;
import com.teemor.common.bean.utils.enums.ResultCode;
import com.teemor.common.bean.utils.tmpl.BaseController;
import com.teemor.common.bean.utils.tmpl.ResponseBen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhoulk
 * @Description: 原子服务API接口实现
 * @date: 2019/11/26 14:50:14
 */
@RestController
public class ArticleController extends BaseController implements IArticleApiService {

    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IArticleExtendService iArticleExtendService;

    @Override
    public String qryArticle(@RequestParam Integer atcId, @RequestParam Integer flag, String ip) {
        return returnData(iArticleService.qryArticleById(atcId, flag, ip));
    }

    @Override
    public String qryArticles(@ModelAttribute QryArticlesIDTo inParam) {
        TbArticleCondition condition = BeanConvertor.convertBean(inParam, TbArticleCondition.class);
        return returnData(iArticleService.qryArticles(condition));
    }

    @Override
    public void updArticle(@RequestBody UpdArticleIDTo inParam) {
        TbArticle tbArticle = BeanConvertor.convertBean(inParam, TbArticle.class);
        iArticleService.updArticleById(tbArticle);
    }

    @Override
    public String addArticle(@RequestBody AddArticleIDTo inParam) {

        iArticleService.addArticle(inParam);
        Map<String, Object> result = new HashMap<>();
        result.put("flag", true);
        return returnData(result);
    }

    @Override
    public String delArticle(@RequestParam Integer atcId) {
        return returnData(iArticleService.delArticleById(atcId) > 0);
    }

    @Override
    public String searchArticles(QryArticlesIDTo inParam, String scrollId) {
        Map<String, Object> map = iArticleService.searchArticles(inParam, scrollId);
        return returnData(map);
    }

    @Override
    public String archive() {
        ResponseBen<List<QryArchivesODTo>> responseBen = new ResponseBen<List<QryArchivesODTo>>(ResultCode.SUCCESS.value(), "", iArticleService.archive());
        return JSON.toJSONStringWithDateFormat(responseBen,"yyyy/MM/dd");
    }

    @Override
    public String updAtcState(@RequestParam Integer atcId, @RequestParam Integer atcState) {
        return returnData(iArticleService.updArticleState(atcId, atcState));
    }

    @Override
    public ResponseBen<Integer> praise(@RequestParam Integer atcId, @RequestParam Integer type, String ip) {
        return returnSuccess(iArticleExtendService.praise(atcId, type, ip));
    }
}